package com.hxalex.simulationhandle.accessbilityService.receiver.search;

import android.util.Log;
import android.view.accessibility.AccessibilityNodeInfo;

import com.hxalex.simulationhandle.accessbilityService.command.base.BaseCommand;
import com.hxalex.simulationhandle.accessbilityService.command.search.CheckBrowserIsMainCommand;
import com.hxalex.simulationhandle.accessbilityService.manger.CommandManger;
import com.hxalex.simulationhandle.accessbilityService.manger.SearchManger;
import com.hxalex.simulationhandle.accessbilityService.receiver.base.BaseReceiver;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by hx_Alex on 2018/5/26.
 */

public class CheckBrowserIsMainReceiver extends BaseReceiver {
    private AccessibilityNodeInfo nodeInfo;
    private boolean isCompleteNowCommand=false;
    private boolean isStartThread=false;
    public CheckBrowserIsMainReceiver(SearchManger searchManger) {
        super(searchManger);
        FLAG="检查浏览器是否进入主页命令";
    }

    public AccessibilityNodeInfo getNodeInfo() {
        return nodeInfo;
    }

    public void setNodeInfo(AccessibilityNodeInfo nodeInfo) {
        this.nodeInfo = nodeInfo;
    }

    @Override
    public void doSomething() {
        if(!isPrintCommandMsg)
        {
            Log.w("CheckBrowserIsMain","开始执行"+FLAG);
            isPrintCommandMsg=true;
        }
        if(nodeInfo==null) return;
        if(!isStartThread)
        {
            isStartThread=true;
            OpenWakeThread thread=new OpenWakeThread();
            thread.start();
        }
      
        searchManger.checkIsSearchMain(nodeInfo);
        if(searchManger.isMain)
        {
            searchManger.isMain=false;
            searchManger.mainKey=new Boolean[]{false,false,false,false,false};
            commandCompleteListener.commandCompleteListener();
            isCompleteNowCommand=true;
            Log.i("CheckBrowserIsMain","已进入百度");
        }
    }
    
    class  OpenWakeThread extends Thread
    {
        @Override
        public void run() {
            while (!isCompleteNowCommand)
            {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                CommandManger commandManger=CommandManger.getManger();
                BaseCommand nowCommand = commandManger.getCommandQueue().peek();
                if(!searchManger.isMain && nowCommand instanceof CheckBrowserIsMainCommand)
                {
                    EventBus.getDefault().post(SearchManger.OPEN_WAKE_WINWODS);
                }
            }
        }
    }
    
    
    
}
